package com.tanggh.action;

import org.springframework.beans.factory.annotation.Autowired;

import com.tanggh.entity.User;
import com.tanggh.service.UserService;
import com.tanggh.util.CookieManager;
import com.tanggh.util.Validator;

public class LoginAction extends GenericAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private UserService userService;
	private String msg;

	@Override
	public String execute() throws Exception {
		String email = getString("email");
		String password = getString("password");
		
		if(Validator.isEmpty(email) || Validator.isEmpty(password)) {
			msg = "请填写正确登陆信息";
		}
		else if(!Validator.isEmail(email)) {
			msg = "Email不正确";
		} else {
			User u = new User();
			u.setEmail(email);
			u.setPassword(password);
			User user = userService.findUser(u);
			if(null != user) { //userId 放入到cookie
				CookieManager.getInstance().addCookis(response, CookieManager.USER_ID, user.getId()+"");
			}else {
				msg = "Email或密码错误";
			}
		}
		return null;
	}
}
